<?php


require('mc_table.php');

//koneksi ke database
$host = "localhost";
$user = "root";
$pass = "";
$dbnm = "db_rental_dev";
 
$conn = mysql_connect($host, $user, $pass);
if ($conn) {
	$open = mysql_select_db($dbnm);
	if (!$open) {
		die ("Database tidak dapat dibuka karena ".mysql_error());
	}
} else {
	die ("Server MySQL tidak terhubung karena ".mysql_error());
}
//akhir koneksi

function DateToIndo($date) { // fungsi atau method untuk mengubah tanggal ke format indonesia
   // variabel BulanIndo merupakan variabel array yang menyimpan nama-nama bulan
		$BulanIndo = array("Januari", "Februari", "Maret",
						   "April", "Mei", "Juni",
						   "Juli", "Agustus", "September",
						   "Oktober", "November", "Desember");
	
		$tahun = substr($date, 0, 4); // memisahkan format tahun menggunakan substring
		$bulan = substr($date, 5, 2); // memisahkan format bulan menggunakan substring
		$tgl   = substr($date, 8, 2); // memisahkan format tanggal menggunakan substring
		
		$result = $tgl . " " . $BulanIndo[(int)$bulan-1] . " ". $tahun;
		return($result);
}

function DateToIndoAngka($date) { // fungsi atau method untuk mengubah tanggal ke format indonesia
   // variabel BulanIndo merupakan variabel array yang menyimpan nama-nama bulan
	
		$tahun = substr($date, 0, 4); // memisahkan format tahun menggunakan substring
		$bulan = substr($date, 5, 2); // memisahkan format bulan menggunakan substring
		$tgl   = substr($date, 8, 2); // memisahkan format tanggal menggunakan substring
		
		$result = $tgl."/".$bulan."/".$tahun;
		return($result);
}


//get query dan variabel
$mulai = $_GET['mulai'];
$akhir = $_GET['akhir'];
$gudang = $_GET['gudang'];
$decode_cr = $_GET['cr_txt'];
$getquery = str_replace("+"," ",$_GET[query]);
$getquerykorporate = str_replace("+"," ",$_GET[query_korporate]);

//instansiasi fpdf
$pdf = new PDF_MC_Table('L','cm','A4');
$pdf->AliasNbPages();  
$pdf->AddPage();
$pdf->SetMargins(0.7,0.7,0.7,0.7);  

 #tampilkan judul laporan
$pdf->SetFont('Arial','B','16');
$pdf->Cell(0,0, "CIPAGANTI RENTAL CAR & TRAVEL", '0', 1, 'C');
$pdf->Cell(0,2, "HASIL SEWA KENDARAAN", '0', 1, 'C');
$pdf->SetFont('Arial','B','12');
$pdf->Cell(3,3, "CABANG : ".$gudang,0,0,'L');
$pdf->Cell(25.2,3, "PERIODE : ".DateToIndo($mulai)." - ".DateToIndo($akhir),0,0,'R');


//membuat kop tabel 
$y=$pdf->GetY();  
$pdf->SetY($y+2.5);  
$pdf->SetFont('Arial','B',11);
$pdf->SetWidths(array(2.3, 2.5, 2.5, 2.5, 3.7, 2.2, 2.2, 2.6, 2.6, 2.6, 2.6));
$pdf->SetHeight(0.1);
$pdf->SetAligns(Array('C','C','C','C','C','C','C','C','C','C','C'));
$pdf->Row(array("CSO", "Berangkat", "No. Order", "No. Kendaraan", "Merk Kendaraan", "Periode Awal", "Periode Akhir", "Customer", "T.Sewa", "T.Bayar", "Piutang"));

$pdf->SetFont('Arial','B',9);
$pdf->SetWidths(array(28.3));
$pdf->Row(array("SEWA KENDARAAN RETAIL"));



//ISI TABEL
$pdf->SetFont('Arial','',9);
$pdf->SetWidths(array(2.3, 2.5, 2.5, 2.5, 3.7, 2.2, 2.2, 2.6, 2.6, 2.6, 2.6));
$pdf->SetHeight(0.1);
$pdf->SetAligns(Array('C','C','C','C','C','C','C','C','C','C','C'));

$query_data = $getquery;
$query = mysql_query ($query_data) or die("Query gagal");

while($result= mysql_fetch_array( $query )){
	
	$pdf->Row(array($result['cso'], DateToIndoAngka($result['checkin']), $result['no_ref'], $result['kendaraan'], $result['merk'].'-'.$result['tipekendaraan'], DateToIndoAngka($result['tgl']), DateToIndoAngka($result['jatuhtempo']), $result['nama'], number_format($result['totalsewa'],0,',','.').",-", number_format($result['totalbayar'],0,',','.').",-", number_format($result['sisakekurangan'],0,',','.').",-" ));
	
	$totalsw = $totalsw + $result['totalsewa'];
    $totalbyr = $totalbyr + $result['totalbayar'];
    $totalsisa = $totalsisa + $result['sisakekurangan'];
	
}

$pdf->SetFont('Arial','B',9);
$pdf->SetWidths(array(20.5,2.6,2.6,2.6));
$pdf->Row(array("SUBTOTAL",number_format($totalsw,0,',','.').",-",number_format($totalbyr,0,',','.').",-",number_format($totalsisa,0,',','.').",-" ));
$pdf->SetWidths(array(28.3));
$pdf->Row(array("SEWA KENDARAAN NON RETAIL"));

$pdf->SetFont('Arial','',9);
$pdf->SetWidths(array(2.3, 2.5, 2.5, 2.5, 3.7, 2.2, 2.2, 2.6, 2.6, 2.6, 2.6));
$pdf->SetHeight(0.1);
$pdf->SetAligns(Array('C','C','C','C','C','C','C','C','C','C','C'));

$query_datakorporate = $getquerykorporate;
$querykorp = mysql_query ($query_datakorporate) or die("Query gagal");

while($resultkorp= mysql_fetch_array( $querykorp )){
	
	$pdf->Row(array($resultkorp['marketing'], DateToIndoAngka($resultkorp['mulai']), $resultkorp['no_ref'], $resultkorp['kendaraan'], $resultkorp['merk'].'-'.$resultkorp['tipekendaraan'], DateToIndoAngka($resultkorp['mulai']), DateToIndoAngka($resultkorp['sampai']), $resultkorp['nama'], number_format($resultkorp['total'],0,',','.').",-", number_format($resultkorp['totalbayar'],0,',','.').",-", number_format($resultkorp['sisakekurangan'],0,',','.').",-" ));
	
	$totalswkorporate = $totalswkorporate + $resultkorp['total'];
    $totalbyrkorporate = $totalbyrkorporate + $resultkorp['totalbayar'];
    $totalsisakorporate = $totalsisakorporate + $resultkorp['sisakekurangan'];
	
}

$pdf->SetFont('Arial','B',9);
$pdf->SetWidths(array(20.5,2.6,2.6,2.6));
$pdf->Row(array("SUBTOTAL",number_format($totalswkorporate,0,',','.').",-",number_format($totalbyrkorporate,0,',','.').",-",number_format($totalsisakorporate,0,',','.').",-" ));
$pdf->SetWidths(array(28.3));
$pdf->Row(array(""));
$pdf->SetWidths(array(20.5,2.6,2.6,2.6));
$pdf->SetFont('Arial','B',10);
$pdf->Row(array("GRAND TOTAL",number_format($totalsw+$totalswkorporate,0,',','.').",-",number_format($totalbyr+$totalbyrkorporate,0,',','.').",-",number_format($totalsisa+$totalsisakorporate,0,',','.').",-" ));

$pdf->Ln();
$pdf->Output('Laporan Hasil Sewa Cabang '.$gudang.' Periode '.DateToIndo($mulai)." - ".DateToIndo($akhir).'.pdf','D');


?>